package br.com.service;

import java.util.List;

import javax.persistence.EntityManager;

import br.com.domain.Domain;
import br.com.exception.PersistenceException;
import br.com.exception.ServiceException;

@SuppressWarnings("unchecked")
public class GenericServiceImpl extends GenericService {
	
	/**************************************************************************/
	/**                                M�TODOS                               **/
	/**************************************************************************/

	@Override
	public Domain salvar(Domain objeto) throws ServiceException { 
		try {
			return genericDAO.salvar(objeto);			
		} catch (PersistenceException e) {			
			throw new ServiceException(e.getMessage(), e);
		}
	}
	
	@Override
	public void salvar(List<? extends Domain> listObjeto) throws ServiceException { 
		try {
			genericDAO.salvar(listObjeto);		
		} catch (PersistenceException e) {			
			throw new ServiceException(e.getMessage(), e);
		}
	}	
	
	@Override
	public void excluir(Domain objeto) throws ServiceException {
		try {
			genericDAO.excluir(objeto);
		} catch (PersistenceException e) {			
			throw new ServiceException(e.getMessage(), e);
		}
	}	
	
	@Override
	public void excluir(List<? extends Domain> listObjeto) throws ServiceException {
		try {
			genericDAO.excluir(listObjeto);
		} catch (PersistenceException e) {			
			throw new ServiceException(e.getMessage(), e);
		}
	}	
	
	@Override
	public Domain buscarPorId(String nomeClasse, Integer id) throws ServiceException {
		try{
			return genericDAO.buscarPorId(nomeClasse, id);
		} catch (PersistenceException e) {			
			throw new ServiceException(e.getMessage(), e);
		}
	}
	
	@Override
	public List<? extends Domain> buscarPorFilter(Domain objeto) throws ServiceException {
		try {
			return genericDAO.buscarPorFilter(objeto);
		} catch (PersistenceException e) {			
			throw new ServiceException(e.getMessage(), e);
		}
	}
	
	@Override
	public  List<? extends Domain> buscarTodos(String nomeClasse, String... parametros) throws ServiceException {
		try {
			return genericDAO.buscarTodos(nomeClasse, parametros);
		} catch (PersistenceException e) {			
			throw new ServiceException(e.getMessage(), e);
		}
	}
	
	@Override
	public  List<? extends Domain> buscarTodos(String nomeClasse,  int startPage, int maxPerPage, String... parametros) throws ServiceException {
		try {
			return genericDAO.buscarTodos(nomeClasse, startPage, maxPerPage, parametros);
		} catch (PersistenceException e) {			
			throw new ServiceException(e.getMessage(), e);
		}
	}
	
	@Override
	public Boolean executarQueryExecute(String query, Object... parametros) throws ServiceException {
		try {
			return genericDAO.executarQueryExecute(query, parametros);
		} catch (PersistenceException e) {			
			throw new ServiceException(e.getMessage(), e);
		}
	}
	

	@Override
	public List<? extends Domain> executarQueryList(String query, Object... parametros) throws ServiceException {
		try {	
			return genericDAO.executarQueryList(query, parametros);
		} catch (PersistenceException e) {			
			throw new ServiceException(e.getMessage(), e);
		}
	}

	@Override
	public Domain executarQuerySingle(String query, Object... parametros) throws ServiceException {
		try {
			return genericDAO.executarQuerySingle(query, parametros);
		} catch (PersistenceException e) {			
			throw new ServiceException(e.getMessage(), e);
		}
	}
	
	@Override
	public Domain buscarUltimoRegistro(String nomeClasse) throws ServiceException {
	try {
		return genericDAO.buscarUltimoRegistro(nomeClasse);
	} catch (PersistenceException e) {			
		throw new ServiceException(e.getMessage(), e);
	}
	}
	

	@Override
	public EntityManager getEntityManager() throws ServiceException {
		return null;
	}

	@Override
	public List<? extends Domain> executarQuerySqlList(String sql,
			String nomeClasse) throws ServiceException {
		return null;
	}

	@Override
	public Domain executarQuerySqlSingle(String sql, String nomeClasse)
			throws ServiceException {
		return null;
	}
	
	/*** PREPARA A QUERY ADICIONANDO OS PARAMETROS DE CONSULTA **/
	public static  String prepareQuery (String sql, Object... parametros) {
        int i = 1; 
        
		/** SETANDO PARAMETROS **/
		for (Object parametro : parametros) {
			sql = sql.replace("?" + i, (String) parametro);
			i++;
		}
		return sql;
	}

	

	

	
	
	


}